﻿using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace Gui.Functies.Agenda
{
    public partial class Agenda : UserControl
    {
        private string currentDate;
        private int projectID;
        private static string conn = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        MySqlConnection grabConnection = new MySqlConnection(conn);
        
        public Agenda()
        {

        }
        //Constructor.
        public Agenda(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
            GetAgenda();
            richTextBox3.Hide();
            btn_confirmedit.Hide();
            label5.Hide();
        }
        //Roept de getNote methode aan als je een agendapunt aanklikt.
        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentDate = (string)listBox1.SelectedItem;
            GetNote();
        }
        //Haalt de lijst met data op wanneer er een agendapunt aangeschreven staat.
        public void GetAgenda()
        {
            try
            {
                MySqlCommand agendaGet = new MySqlCommand("SELECT date FROM agenda WHERE projectID=@projectID", grabConnection);
                agendaGet.Parameters.Add(new MySqlParameter("projectID", projectID));
                grabConnection.Open();
                MySqlDataReader reader = agendaGet.ExecuteReader();

                while (reader.Read())
                {
                    string date = reader.GetString(0);
                    listBox1.Items.Add(date);
                }

                reader.Dispose();
                agendaGet.Dispose();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {   
                grabConnection.Close();
            }
        }
        //Haalt de note op die bij het agendapunt zit.
        public void GetNote()
        {
            try
            {
                MySqlCommand noteGet = new MySqlCommand("SELECT note FROM agenda WHERE projectID=@projectID AND date=@currentDate", grabConnection);
                noteGet.Parameters.Add(new MySqlParameter("projectID", projectID));
                noteGet.Parameters.Add(new MySqlParameter("currentDate", currentDate));
                grabConnection.Open();

                MySqlDataReader reader = noteGet.ExecuteReader();

                while (reader.Read())
                {
                    string note = reader.GetString(0);
                    richTextBox2.Text = note;
                }

                reader.Dispose();
                noteGet.Dispose();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                grabConnection.Close();
            }
        }
        //Opent et schemr om een nieuw agendapunt te maken.
        private void btn_nieuwpunt_Click(object sender, EventArgs e)
        {
            Controls.Clear();
            Controls.Add(new Agendapunten(projectID));
        }
        //Methode om de note aan te passen van een agendapunt.
        public void EditNote(string date)
        {
            string changeDate = date;
            string newNote = richTextBox3.Text;

            if (changeDate != "")
            {
                MySqlCommand agenda = new MySqlCommand("UPDATE agenda SET note=@note WHERE date=@date", grabConnection);
                agenda.Parameters.Add(new MySqlParameter("date", changeDate));
                agenda.Parameters.Add(new MySqlParameter("note", newNote));

                try
                {
                    grabConnection.Open();
                    agenda.ExecuteNonQuery();
                }
                catch (MySqlException e)
                {
                    MessageBox.Show(e.ToString());
                }
                finally
                {
                    grabConnection.Close();
                    Controls.Clear();
                    Controls.Add(new Agenda(projectID));
                }
            }
            else
            {
                MessageBox.Show("Geen nieuwe note ingesteld");
            }
        }
        //Button waarmee je zorgt dat de edit wordt opgeslagen.
        private void btn_confirmedit_Click(object sender, EventArgs e)
        {
            EditNote(currentDate);
            richTextBox3.Hide();
            richTextBox2.Show();
            btn_confirmedit.Hide();
            btn_edit.Show();
        }
        //Opent de edit box om een punt aan te passen.
        private void btn_edit_Click(object sender, EventArgs e)
        {
            richTextBox2.Hide();
            richTextBox3.Show();
            label4.Hide();
            label5.Show();
            btn_edit.Hide();
            btn_confirmedit.Show();
            richTextBox3.Text = richTextBox2.Text;
        }
    }
}
